/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.caste_tagprocess;

import com.apt.tag.Connect_DB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Bach
 */
public class UpdateCaste {

    public static boolean updateCaste(String castename, String id) {
        String up = " UPDATE [Matrimony].[dbo].[Castes]\n"
                + "   SET [Caste_Name] =?\n"
                + " WHERE [Caste_Id]=? and [Caste_status]=1";
        boolean re = false;
        Connection con = null;
        PreparedStatement ps = null;
        if (castename == null) {
            return false;
        }
        try {
            con = Connect_DB.getConnection();
            if (checkName(castename, con)) {
                ps = con.prepareStatement(up);
                ps.setString(1, castename);
                ps.setString(2, id);
                int i = ps.executeUpdate();
                if (i > 0) {
                    re = true;
                }
            } else {
                System.out.println("Caste Name is Exist");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Connect_DB.close(null, null, ps, con);
        }
        return re;
    }

    private static boolean checkName(String name, Connection cnn) {
        boolean re = false;
        PreparedStatement ps;
        String sql = " SELECT [Caste_Name] "
                + "  FROM [Matrimony].[dbo].[Castes]"
                + "  where [Caste_Name]=?";
        ResultSet rs = null;
        try {
            ps = cnn.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            if (rs.next()) {
                re = false;
            } else {
                re = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }
}
